<?php
/*******************************************************************************
Rd/Request.php
Implements Reserve Request utilities for RD

Created by Troy Hurteau, NCSU Libraries, NC State University (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/

require_once(APPLICATION_PATH . '/lib/Rd/Request/Status.php');
require_once(APPLICATION_PATH . '/lib/Rd/Dao/Request.php');

/**
 * 
 * Request utility object.
 * @author jthurtea
 *
 */

class Rd_Request{
	
	protected static $_typeOptions = array('PHYSICAL', 'SCAN');
	
	public static function create($requestData, $item=NULL, $course=NULL, $user=NULL)
	{
		if (!is_null($item) && is_a($item, 'item')) {
			$requestItem = $item;
		}
		//#TODO handle the case where $item is an integer (item_id), or when it is in $requestData
		if (!is_null($course) && is_a($course, 'course')) {
			$requestCourse = $course;
		}
		//#TODO handle the case where $course is an integer (course_id), or when it is in $requestData
		if (!is_null($user) && is_a($user, 'user')) {
			$requestUser = $user;
		} else {
			$requestUser = Account_Rd::getUserInterface();
		} //#TODO handle case when $user is a userID we probably don't want to accept this value directly from $requestData...
		
		$requestData['initialStatus'] = Rd_Request_Status::getNewStatusId();
		$reserve = Rd_Reserve::create($requestData, $requestItem, $requestCourse);
		$itemId = $item->getId();
		$notesKey = (
			array_key_exists("notes_{$itemId}", $requestData)
			? "notes_{$itemId}"
			: 'notes'
		);
		$notes = (
			Rd_Array::keyExistsAndNotBlank($notesKey, $requestData)
			? trim($requestData[$notesKey])
			: ''
		);
		unset($requestData['notes']);
		if ($reserve->isInstantiated()) {
			$request = new request();
			//prevent duplicate requests
			if ( //#TODO this is a messy way to do it...
				$request->getRequestByCI_Item($course->getCourseInstanceID(), $itemId) === false
			) {
				$request->createNewRequest($course->getCourseInstanceID(), $itemId);
				$request->setRequestingUser($requestUser->getUserID());
				$request->setReserveID($reserve->getReserveID());
				$dateNeededKey = (
					array_key_exists("dateNeeded_{$itemId}",$requestData)
					? "dateNeeded_{$itemId}"
					: 'dateNeeded'
				);
				if( 
					array_key_exists($dateNeededKey, $requestData)
					&& '' != trim($dateNeededKey)
				){
					$request->setDateDesired($requestData[$dateNeededKey]);
				}

				if('' != $notes){
					$request->setNote($notes, note::TYPE_STAFF);
				}
				$requestTypeKey = (
					array_key_exists("requestType_{$itemId}",$requestData)
					? "requestType_{$itemId}"
					: 'requestType'
				);
				$requestTypeValue = 
					array_key_exists($requestTypeKey, $requestData) 
						&& '' != trim($requestData[$requestTypeKey])
					? trim(strtoupper($requestData[$requestTypeKey]))
					: 'PHYSICAL';
				if ('ELECTRONIC' == $requestTypeValue) {
					$requestTypeValue = 'SCAN';
				}
				$request->setType(
					in_array($requestTypeValue, self::$_typeOptions)
					? $requestTypeValue
					: 'PHYSICAL'
				);
				//#TODO currently does not support multiple values
				if(array_key_exists('homeLibrary',$requestData) 
					&& '' != trim($requestData['homeLibrary'])
					&& 'electronic' != strtolower($request->getType())
				){
					$item->setHomeLibraryID($requestData['homeLibrary']);
				} else {
					$item->setHomeLibraryID(library::getDefaultElectronicLibrary()->getId());
				}
			}
			return $request;
		} else {
			throw new Exception('Unable to create the reserve for this request');
		}
	}	
}